﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCust_BusNatureDAL:ItblCust_BusNature
    {
        HKEA_CMSEntities model;
        tblCust_BusNature busnature;
         
        public tblCust_BusNatureDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCust_BusNatureDAL GetInstance()
        {
            return new tblCust_BusNatureDAL();
        }

        public int InsertENT(tblCust_BusNature ent)
        {
            var temp = model.tblCust_BusNature.Where(a=>a.CustomerUID==ent.CustomerUID).Where(a=>a.BusNatureUID==ent.BusNatureUID).Where(a=>a.Deleted==0);
            if (temp == null)
            {
                ent.CreationDate = DateTime.Now;
                ent.LastModifyDate = DateTime.Now;
                model.tblCust_BusNature.AddObject(ent);
            }
            return model.SaveChanges();
        }

        public int SaveList(List<int> toAddList, List<int> toDelList, int customeruid,string LastModifyBy)
        {
            foreach (var temp in toAddList)
            {
                var tempValue = model.tblCust_BusNature.Where(a => a.CustomerUID == customeruid).
                    Where(a => a.BusNatureUID == temp).FirstOrDefault();
                var ent = new tblCust_BusNature();
                if (tempValue == null)
                {
                    ent.BusNatureUID = temp;
                    ent.CustomerUID = customeruid;
                    ent.CreationDate = DateTime.Now;
                    ent.Deleted = 0;
                    ent.LastModifyBy = LastModifyBy;
                    ent.LastModifyDate = DateTime.Now;
                    model.tblCust_BusNature.AddObject(ent);
                }
                else
                {
                    if (tempValue.Deleted == 1)
                    {
                        tempValue.Deleted = 0;
                        tempValue.LastModifyDate = DateTime.Now;
                        tempValue.LastModifyBy = LastModifyBy;
                    }
                }
            }
            foreach (var temp in toDelList)
            {
                var result = model.tblCust_BusNature.Where(a => a.CustomerUID == customeruid).
                    Where(a => a.BusNatureUID == temp).
                    Where(a => a.Deleted == 0).FirstOrDefault();
                if (result != null)
                {
                    result.LastModifyDate = DateTime.Now;
                    result.LastModifyBy = LastModifyBy;
                    result.Deleted = 1;
                }
            }
            return model.SaveChanges();
        }

        public int InsertENT(List<int> BusNatureUIDList, int CustomerUID, string LastModifyBy)
        {
            var toDel = model.tblCust_BusNature.Where(a => a.CustomerUID == CustomerUID);
            if (toDel.Count() > 0)
            {
                foreach (var temp in toDel)
                {
                    model.tblCust_BusNature.DeleteObject(temp);
                }
            }
            foreach (var busNatureUID in BusNatureUIDList)
            {
                var tempENT = new tblCust_BusNature();
                tempENT.BusNatureUID = busNatureUID;
                tempENT.CustomerUID = CustomerUID;
                tempENT.Deleted = 0;
                tempENT.LastModifyBy = LastModifyBy;
                tempENT.CreationDate = DateTime.Now;
                tempENT.LastModifyDate = DateTime.Now;
                model.tblCust_BusNature.AddObject(tempENT);
            }
            return model.SaveChanges();
        }

        public List<tblCust_BusNature> GetListByENT(tblCust_BusNature ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCust_BusNature.Where(a => a.Cust_BusNatureUID > 0).Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Cust_BusNatureUID > 0)
                result = result.Where(a => a.Cust_BusNatureUID == ent.Cust_BusNatureUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblCust_BusNature> GetListByENT(tblCust_BusNature ent, bool NeedDelete)
        {
            var result = model.tblCust_BusNature.Where(a => a.Cust_BusNatureUID > 0);
            if(NeedDelete)
                result=result.Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Cust_BusNatureUID > 0)
                result = result.Where(a => a.Cust_BusNatureUID == ent.Cust_BusNatureUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblCust_BusNature ent,bool NeedDelete)
        {
            var result = model.tblCust_BusNature.Where(a => a.Cust_BusNatureUID > 0);
            if (NeedDelete)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.Cust_BusNatureUID > 0)
                result = result.Where(a => a.Cust_BusNatureUID == ent.Cust_BusNatureUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblCust_BusNature ent)
        {
            busnature = model.tblCust_BusNature.Where(a=>a.Cust_BusNatureUID==ent.Cust_BusNatureUID).FirstOrDefault();
            if (busnature != null)
            {
                busnature.Deleted = ent.Deleted;
                busnature.LastModifyBy = ent.LastModifyBy;
                busnature.LastModifyDate = DateTime.Now;
                busnature.BusNatureUID = ent.BusNatureUID;
                busnature.CustomerUID = ent.CustomerUID;
                busnature.Remark = ent.Remark;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblCust_BusNature ent)
        {
            var temp = model.tblCust_BusNature.Where(a => a.Cust_BusNatureUID > 0);
            if (ent.Cust_BusNatureUID > 0)
                temp = temp.Where(a=>a.Cust_BusNatureUID==ent.Cust_BusNatureUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCust_BusNature.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
